package com.engine4cloud.tdf.supervision.convert;

import com.engine4cloud.tdf.common.core.constant.SupervisionBusinessConstants;
import com.engine4cloud.tdf.common.core.constant.enums.AllAuditActionStatusEnum;
import com.engine4cloud.tdf.common.core.constant.enums.OutEscalationStatusEnum;
import com.engine4cloud.tdf.common.core.util.CodeUtils;
import com.engine4cloud.tdf.common.core.util.lambda.Actions;
import com.engine4cloud.tdf.supervision.dto.create.SpOutApplicationCreateDTO;
import com.engine4cloud.tdf.supervision.dto.result.SpOutApplicationResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.SpOutApplicationUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.SpOutApplicationEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Mapper
public interface SpOutApplicationConvert {
    SpOutApplicationConvert INSTANCE = Mappers.getMapper(SpOutApplicationConvert.class);

    SpOutApplicationResultDTO convertTo(SpOutApplicationEntity s);

    SpOutApplicationEntity createTo(SpOutApplicationCreateDTO s);

    SpOutApplicationEntity updateTo(SpOutApplicationUpdateDTO s);

    SpOutApplicationEntity close(SpOutApplicationResultDTO outApplication);

    default SpOutApplicationEntity create(SpOutApplicationCreateDTO s, Long tenantId, AllAuditActionStatusEnum action, BigDecimal duration, Integer range) {
        SpOutApplicationEntity entity = createTo(s);
        entity.setTenantId(tenantId);
        entity.setPreAction(action);
        entity.setLcbh(CodeUtils.entrustCode());
        entity.setDuration(duration.add(s.getWcts()));
        if (s.getWcts().compareTo(new BigDecimal(range)) > 0) {
            entity.setEscalation(OutEscalationStatusEnum.UNPROCESSED);
        } else {
            entity.setEscalation(OutEscalationStatusEnum.NONE);
        }

        additionalInformation(entity, s.getDestinationList());
        return entity;
    }

    default SpOutApplicationEntity update(SpOutApplicationUpdateDTO s, BigDecimal oldDuration, BigDecimal oldDays, Integer range) {
        SpOutApplicationEntity entity = updateTo(s);
        additionalInformation(entity, s.getDestinationList());
        entity.setDuration(oldDuration.add(s.getWcts().subtract(oldDays)));
        if (s.getWcts().compareTo(new BigDecimal(range)) > 0) {
            entity.setEscalation(OutEscalationStatusEnum.UNPROCESSED);
        } else {
            entity.setEscalation(OutEscalationStatusEnum.NONE);
        }
        return entity;
    }

    default SpOutApplicationResultDTO convert(SpOutApplicationEntity s) {
        if (Objects.isNull(s)) {
            return null;
        }

        SpOutApplicationResultDTO result = convertTo(s);

        List<String> destinationList = new ArrayList<>();
        destinationList.add(s.getWcmddszs());
        destinationList.add(s.getWcmddszd());
        Actions.accept(s.getWcmddszx(), v -> destinationList.add(s.getWcmddszx()));
        Actions.accept(s.getWcmddxz(), v -> destinationList.add(s.getWcmddxz()));
        result.setDestinationList(destinationList);
        return result;
    }

    default void additionalInformation(SpOutApplicationEntity t, List<String> destinationList) {
        if (Objects.isNull(t) || CollectionUtils.isEmpty(destinationList)) {
            return;
        }

        t.setWcmddszs(destinationList.get(SupervisionBusinessConstants.AREA_PROVINCE_INDEX));
        t.setWcmddszd(destinationList.get(SupervisionBusinessConstants.AREA_CITY_INDEX));

        if (destinationList.size() >= SupervisionBusinessConstants.AREA_COUNTY_ARRAY_LENGTH) {
            t.setWcmddszx(destinationList.get(SupervisionBusinessConstants.AREA_COUNTY_INDEX));
        } else {
            t.setWcmddszx(null);
        }

        if (destinationList.size() >= SupervisionBusinessConstants.AREA_VILLAGE_ARRAY_LENGTH) {
            t.setWcmddxz(destinationList.get(SupervisionBusinessConstants.AREA_VILLAGE_INDEX));
        } else {
            t.setWcmddxz(null);
        }
    }


}
